home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / edm0407s.zip / STAT3SRC.ZIP / AFILE.CPP < prev    next >
Text File  |  1996-07-22  |  10KB  |  242 lines

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // AFILE.CPP - description of AFile, base file class
  4. //
  5. ////////////////////////////////////////////////////////////////////////////
  6.  
  7. #include "afile.hpp"
  8.  
  9. //**************************************************************************
  10. // AFile :: AFile - Constructor of AFile                                   *
  11. //**************************************************************************
  12. AFile :: AFile (PVOID pBuffer, ULONG ulBufferSize)
  13.                 : m_ulBufferSize (ulBufferSize),  // initialize m_ulBufferSize and m_pBuffer
  14.                   m_pBuffer (pBuffer)
  15.  
  16. {
  17.   m_bIsOpen = false;        // reset the variables
  18.  
  19.   m_pFilestatus3 = 0L;
  20.  
  21.   m_pisFileName = 0L;
  22.  
  23.   m_hFile = 0;
  24.  
  25.   m_ulActionTaken =
  26.   m_ulFileAttribute,
  27.   m_ulOpenFlag,
  28.   m_ulOpenMode,
  29.   m_ulBytesRead,
  30.   m_ulBytesWritten = 0UL;
  31.  
  32.   m_lastDosError = 0;
  33. }
  34.  
  35. //**************************************************************************
  36. // AFile :: ~AFile - Destructor of AFile                                   *
  37. //**************************************************************************
  38. AFile :: ~AFile(void)
  39.  
  40. {
  41.   if(m_bIsOpen)  // is the file still open
  42.      {                    
  43.       close ();  // yes, close it
  44.      }
  45.  
  46.   if(m_pisFileName)          // is the IString allocated?
  47.      {
  48.       delete(m_pisFileName); // yes, delete it
  49.       m_pisFileName = 0L;
  50.      }
  51.  
  52.   if(m_pFilestatus3)         // is the FILESTATUS3 allocated
  53.      {
  54.       delete m_pFilestatus3; // yes, delete it
  55.       m_pFilestatus3 = 0L;
  56.      }
  57. }
  58.  
  59. //**************************************************************************
  60. // AFile :: open - open the file                                           *
  61. //**************************************************************************
  62. APIRET AFile :: open (const PSZ pszFileName, const ULONG ulFileSize, const ULONG ulFileAttribute,
  63.                       const ULONG ulOpenFlag, const ULONG ulOpenMode, const PEAOP2 pEABuf)
  64.  
  65. {
  66.   IString fileName (pszFileName);  // create an IString and call open
  67.  
  68.   return (open(fileName, ulFileSize, ulFileAttribute, ulOpenFlag, ulOpenMode, pEABuf));
  69. }
  70.  
  71. //**************************************************************************
  72. // AFile :: open - open the file                                           *
  73. //**************************************************************************
  74. APIRET AFile :: open (const IString & fileName, const ULONG ulFileSize, const ULONG ulFileAttribute,
  75.                       const ULONG ulOpenFlag, const ULONG ulOpenMode, const PEAOP2 pEABuf)
  76.  
  77. {
  78.   APIRET ret;
  79.  
  80.   if(m_bIsOpen)           // is the file already open?
  81.      return ((APIRET) 0); // yes, return 0
  82.  
  83.   if(!m_pisFileName)                          // is the IString allocated
  84.      m_pisFileName = new IString (fileName);  // no, allocate it
  85.   else
  86.      *m_pisFileName = fileName;               // yes, reset the stored filename
  87.  
  88.   m_ulFileAttribute = ulFileAttribute;        // store the attributes
  89.   m_ulOpenFlag      = ulOpenFlag;             // store the flags
  90.   m_ulOpenMode      = ulOpenMode;             // store the mode
  91.  
  92.   ret = DosOpen(*m_pisFileName, &m_hFile, &m_ulActionTaken, ulFileSize,   // try to open the file
  93.                 m_ulFileAttribute, m_ulOpenFlag, m_ulOpenMode, pEABuf);
  94.  
  95.   if (!ret)             // open was successful?
  96.       m_bIsOpen = true; // yes, set the flag to true
  97.  
  98.   return (ret);         // return the return-value of DosOpen
  99. }
  100.  
  101. //**************************************************************************
  102. // AFile :: reOpen - re-open a file                                        *
  103. //**************************************************************************
  104. APIRET AFile :: reOpen (const ULONG ulFileSize, const ULONG ulFileAttribute,
  105.                         const ULONG ulOpenFlag, const ULONG ulOpenMode, const PEAOP2 pEABuf)
  106.  
  107. {
  108.   if(!m_pisFileName)                    // do we have a filename
  109.      return (AFILE_FILENAME_MISSING);   // no, return
  110.  
  111.   return (open(*m_pisFileName, ulFileSize, ulFileAttribute, ulOpenFlag, ulOpenMode, pEABuf)); // open the file
  112. }
  113.  
  114. //**************************************************************************
  115. // AFile :: close - close the file                                         *
  116. //**************************************************************************
  117. APIRET AFile :: close(void)
  118.  
  119. {
  120.   APIRET ret;
  121.  
  122.   if(!m_bIsOpen)                  // is the file open?
  123.      return(AFILE_FILE_NOT_OPEN); // no, return
  124.  
  125.   ret = DosClose(m_hFile);        // close the file
  126.  
  127.   if(!ret)                        // close successful?
  128.       m_bIsOpen = false;          // yes, reset the flag
  129.  
  130.   return(ret);                    // return the return-value of DosClose
  131. }
  132.  
  133. //**************************************************************************
  134. // AFile :: read - read the data                                           *
  135. //**************************************************************************
  136. APIRET AFile :: read(PVOID pBuffer, ULONG ulBufferSize)
  137.  
  138. {
  139.   APIRET ret = checkReadWrite (pBuffer, ulBufferSize); // check if all parameters are set properly
  140.  
  141.   if(ret)                                                             // all parameters ok?
  142.      m_ulBytesRead = 0UL;                                             // no, don't read
  143.   else
  144.      ret = DosRead (m_hFile, (pBuffer ? pBuffer : m_pBuffer),         // yes, read the data
  145.                     (ulBufferSize ? ulBufferSize : m_ulBufferSize),
  146.                     &m_ulBytesRead);
  147.  
  148.   return (ret);  // return the value of checkReadWrite / DosRead
  149. }
  150.  
  151. //**************************************************************************
  152. // AFile :: write - writes the data                                        *
  153. //**************************************************************************
  154. APIRET AFile :: write(PVOID pBuffer, ULONG ulBufferSize)
  155.  
  156. {
  157.   APIRET ret = checkReadWrite (pBuffer, ulBufferSize); // check if all parameters are set properly
  158.  
  159.   if(ret)                                                             // all parameters ok?
  160.      m_ulBytesWritten = 0UL;                                          // no, don't write
  161.   else
  162.      ret = DosWrite (m_hFile, (pBuffer ? pBuffer : m_pBuffer),        // yes, write the data
  163.                      (ulBufferSize ? ulBufferSize : m_ulBufferSize),
  164.                      &m_ulBytesWritten);
  165.  
  166.   return (ret);  // return the value of checkReadWrite / DosWrite
  167. }
  168.  
  169. //**********************************************************************************************
  170. // AFile :: checkReadWrite- checks if the buffer and it's size are set and if the file is open *
  171. //**********************************************************************************************
  172. APIRET AFile :: checkReadWrite(const PVOID pBuffer, const ULONG ulBufferSize)
  173.  
  174. {
  175.   if(!m_bIsOpen)                          // is the file open
  176.      return (AFILE_FILE_NOT_OPEN);      // no, return
  177.  
  178.   if(!pBuffer                           // is a pointer to a buffer set?
  179.      && !m_pBuffer)
  180.      return (AFILE_NO_BUFFER_POINTER);  // no, return
  181.  
  182.   if(!ulBufferSize                      // is the size of the buffer set?
  183.      && !m_ulBufferSize)
  184.      return (AFILE_NO_BUFFER_SIZE);     // no, return
  185.  
  186.   return ((APIRET) 0);                  // everything is ok
  187. }
  188.  
  189. //**************************************************************************
  190. // AFile :: queryFileInfo - query the file info level 1                    *
  191. //**************************************************************************
  192. APIRET AFile :: queryFileInfo (Boolean bRefresh)
  193.  
  194. {
  195.   if(!m_bIsOpen)                        // is the file open?
  196.      {
  197.       if(bRefresh)                       // no; is refresh demanded?
  198.          {
  199.           return (AFILE_FILE_NOT_OPEN);   // yes, but can't refresh because the file is not open, return
  200.          }
  201.       else                               // no refresh demanded
  202.          {
  203.           if(!m_pFilestatus3)            // did i query the file info before?
  204.              {
  205.               return (AFILE_FILE_NOT_OPEN); // no, but can't do it now because the file is not open, return
  206.              }
  207.           else                           // got the file info before
  208.              {
  209.               return (NO_ERROR);         // return
  210.              }
  211.          }
  212.      }
  213.  
  214.   // the file is open
  215.  
  216.   if(!m_pFilestatus3)                         // did i query the file info before?
  217.      {                                        // no
  218.       if(!(m_pFilestatus3 = new FILESTATUS3)) // allocte the buffer
  219.          return (ERROR_BUFFER_OVERFLOW);
  220.       return (DosQueryFileInfo (m_hFile, FIL_STANDARD, m_pFilestatus3, sizeof(FILESTATUS3))); // query the file info and return
  221.      }
  222.  
  223.   if(!bRefresh)                               // shall i re-query?
  224.      return (NO_ERROR);                       // no, return
  225.  
  226.   return (DosQueryFileInfo (m_hFile, FIL_STANDARD, m_pFilestatus3, sizeof(FILESTATUS3)));  // query the file info and return
  227. }
  228.  
  229. //**************************************************************************
  230. // AFile :: getFileSize - return the size of the file                      *
  231. //**************************************************************************
  232. ULONG AFile :: getFileSize (Boolean bRefresh)
  233.  
  234. {
  235.   m_lastDosError = queryFileInfo (bRefresh); // query the file info
  236.  
  237.   if(m_lastDosError)                         // error?
  238.      return (0UL);                           // yes, return 0UL
  239.   else
  240.      return (m_pFilestatus3 -> cbFile);        // no, return the size of the file
  241. }
  242.